package org.sy.lab.多线程基础._1_管理._2_状态切换;

import java.util.concurrent.TimeUnit;

/**
 * 线程状态：NEW、RUNNABLE、BLOCKED、WAITING、TIME_WAITING、TERMINATED
 */
public class Main {

	public static void main(String[] args) {
		
		try {
			
			Object lock = new Object();
			System.out.println("线程运行：创建线程");
			Thread myThread = new Thread(new MyThread(lock));
			TimeUnit.SECONDS.sleep(1);
			
			//NEW
			System.out.println("线程状态：" + myThread.getState());
			TimeUnit.SECONDS.sleep(1);
			
			//启动线程然后等待lock
			System.out.println("线程运行：启动线程");
			myThread.start();
			TimeUnit.SECONDS.sleep(2);
			
			//WAITING
			System.out.println("线程状态：" + myThread.getState());
			TimeUnit.SECONDS.sleep(1);
			
			//启动解锁线程唤醒lock
			//TIME_WAITING
			Thread unLockThread = new Thread(new UnLockThread(lock));
			unLockThread.start();
			TimeUnit.SECONDS.sleep(2);
			System.out.println("线程状态：" + myThread.getState());
			
			TimeUnit.SECONDS.sleep(1);
			Thread lockThread = new Thread(new LockThread(lock));
			lockThread.start();
			
			//BLOCKED
			TimeUnit.SECONDS.sleep(2);
			System.out.println("线程状态：" + myThread.getState());
			
			//RUNNABLE
			TimeUnit.SECONDS.sleep(4);
			System.out.println("线程状态：" + myThread.getState());
			
			TimeUnit.SECONDS.sleep(1);
			System.out.println("线程运行：中断线程");
			myThread.interrupt();
			
			//TERMINATED
			TimeUnit.SECONDS.sleep(2);
			System.out.println("线程状态：" + myThread.getState());
		
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
